package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.specification.TbSpecificationOption;
import com.dongdongshop.service.SpecificationOptionServiceI;
import com.dongdongshop.service.SpecificationServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 规格管理
 */
@Controller
@RequestMapping("spec")
public class SpecificationController {

    @Reference
    private SpecificationServiceI specificationServiceI;
 //   @Reference
   // private SpecificationOptionServiceI specificationOptionServiceI;

    private final Logger logger= LoggerFactory.getLogger(SpecificationController.class);

    @RequestMapping("showSpec")
    public String  showSpec(){
        return "admin/specification";
    }

    @RequestMapping("getSpecName")
    @ResponseBody
    public Result<List<TbSpecification>> getSpecName(@RequestParam("specName") String specName){
        try {
            List<TbSpecification> specificationList=specificationServiceI.getSpecName(specName);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(specificationList);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("deleteSpec/{ids}")
    @ResponseBody
    public Result deleteSpec(@PathVariable("ids")Integer[] ids){
        try {
            int specIds=specificationServiceI.deleteSpec(ids);
            if (specIds > 0){
                return Result.jsonResponse(HttpResponseEnum.OK);
            }else {
                return Result.jsonResponse(HttpResponseEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("insertSpecs")
    @ResponseBody
    public Result insertSpecs(TbSpecification specification,String json){
        try {
            //JSON.parse()方法将json字符串转化为Javascript值或对象。
            List<TbSpecificationOption>  specificationOptions =(List<TbSpecificationOption>) JSON.parse(json);
           int count= specificationServiceI.insertSpecs(specification,specificationOptions);
            if (count > 0){
                return Result.jsonResponse(HttpResponseEnum.OK);
            }else {
                return Result.jsonResponse(HttpResponseEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("getSpecById/{id}")
    @ResponseBody
    public Result<TbSpecification> getSpecById(@PathVariable("id")Long id){
        try {
            TbSpecification specification=specificationServiceI.getSpecById(id);
            return Result.jsonResponse(HttpResponseEnum.OK).setData(specification);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

    @RequestMapping("updateSpec")
    @ResponseBody
    public Result updateSpec(TbSpecification specification,String json){
        System.out.println("ss:"+specification+"22:"+json);
        try {
            List<TbSpecificationOption> specificationOptions =(List<TbSpecificationOption>) JSON.parse(json);
            int spec=specificationServiceI.updateSpec(specification,specificationOptions);
            if (spec > 0){
                return Result.jsonResponse(HttpResponseEnum.OK);
            }else {
                return Result.jsonResponse(HttpResponseEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }
}
